/**
 * 作者: clz
 * 日期: 2025-05-07 02:12:43
 * 备注: 联盟运输中心信息
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUnionBusinessTaskInfo;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UnionBusinessTaskInfoModel extends BaseModel implements Model {
	public static final String LISTKEY = "";
	public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
	public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
	public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

	protected long id; // 联盟id
	protected int taskId; // 运输中心订单id (关联UnionBusiness表id)
	protected int num; // 订单进度

	public void reset() {
	}

	protected String getKey() {
		return "UnionBusinessTaskInfo." + id;
	}

	protected String getListKey() {
		return null;
	}

	protected String getListItemKey() {
		return null;
	}

	public void doCreate() {
		setTableId();
		insert();
	}

	public void setTableId() {
		putId(incrTableId("union_business_task_info"));
	}

	public JsonObject insert() {
		return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
	}

	public void delete() {
		super.delete(getKey(), id, UPDATE_DB_TYPE, 0, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
	}

	/**
	 * 根据入库类型更新
	 */
	public JsonObject update() {
		return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
	}

	/**
	 * 实时入队列
	 */
	public JsonObject updateQueueNow() {
		return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
	}

	public JsonObject getAllObj(){
		JsonObject obj = new JsonObject();
		obj.put("id", id); // 联盟id
		obj.put("taskId", taskId); // 运输中心订单id (关联UnionBusiness表id)
		obj.put("num", num); // 订单进度
		return obj;
	}

	public PBUnionBusinessTaskInfo.Builder takeInitBuilder() {
		PBUnionBusinessTaskInfo.Builder sb = PBUnionBusinessTaskInfo.newBuilder();
		if (taskId != 0) {
			sb.setTaskId(taskId);
		}
		if (num != 0) {
			sb.setNum(num);
		}
		return sb;
	}

	public PBUnionBusinessTaskInfo.Builder takeBuilder() {
		if (updateObj == null || updateObj.isEmpty()) {
			return takeInitBuilder();
		}
		PBUnionBusinessTaskInfo.Builder sb = PBUnionBusinessTaskInfo.newBuilder();
		if (isFieldUpdate("taskId")) {
			sb.setTaskId(taskId);
		}
		if (isFieldUpdate("num")) {
			sb.setNum(num);
		}
		return sb;
	}

	public long getId() {
		return id;
	}

	public void putId(long id) {
		if (this.id != id) {
			this.id = id;
			updateRedisHashField(getKey(), "id", this.id);
		}
	}

	public void addId(long value) {
		this.id += value;
		updateRedisHashField(getKey(), "id", this.id);
	}

	public int getTaskId() {
		return taskId;
	}

	public void putTaskId(int taskId) {
		if (this.taskId != taskId) {
			this.taskId = taskId;
			updateRedisHashField(getKey(), "taskId", this.taskId);
		}
	}

	public void addTaskId(int value) {
		this.taskId += value;
		updateRedisHashField(getKey(), "taskId", this.taskId);
	}

	public int getNum() {
		return num;
	}

	public void putNum(int num) {
		if (this.num != num) {
			this.num = num;
			updateRedisHashField(getKey(), "num", this.num);
		}
	}

	public void addNum(int value) {
		this.num += value;
		updateRedisHashField(getKey(), "num", this.num);
	}

}